//
//  SAXXMLParser.m
//  XML解析作业
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SAXXMLParser.h"
#import "AlbumInfoModel.h"

@interface SAXXMLParser ()<NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, strong) NSMutableArray *contentArray;

@end

@implementation SAXXMLParser

+ (NSArray *)saxXmlParserWithXMLFile:(NSString *)file fileType:(NSString *)fileType
{
    SAXXMLParser *sax = [[SAXXMLParser alloc]init];
    sax.array = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle]pathForResource:file ofType:fileType];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = sax;
    [parser parse];
    return sax.array;
}
/** 开始解析 */
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.array = [NSMutableArray array];
}

/** 开始标签 */
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"AlbumInfo"]) {
        AlbumInfoModel *alumInfo = [[AlbumInfoModel alloc]init];
        [self.array addObject:alumInfo];
    }
    if ([elementName isEqualToString:@"actors"] || [elementName isEqualToString:@"directors"] || [elementName isEqualToString:@"categories"] || [elementName isEqualToString:@"itemCodes"]) {
        self.contentArray = [NSMutableArray array];
    }
}

/** 标签之间的值 */
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([string containsString:@"\n    "]) {
        self.content = nil;
        return;
    }
    self.content = string;
    [self.contentArray addObject:string];
}

/** 结束标签 */
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    AlbumInfoModel *albumInfo = [self.array lastObject];
    if ([elementName isEqualToString:@"name"]) {
        albumInfo.name = self.content;
    } else if ([elementName isEqualToString:@"albumCode"]) {
        albumInfo.albumCode = self.content;
    } else if ([elementName isEqualToString:@"picUrl"]) {
        albumInfo.picUrl = self.content;
    } else if ([elementName isEqualToString:@"middlePicurl"]) {
        albumInfo.middlePicurl = self.content;
    } else if ([elementName isEqualToString:@"playUrl"]) {
        albumInfo.playUrl = self.content;
    } else if ([elementName isEqualToString:@"transitionUrl"]) {
        albumInfo.transitionUrl = self.content;
    } else if ([elementName isEqualToString:@"playlistCode"]) {
        albumInfo.playlistCode = self.content;
    } else if ([elementName isEqualToString:@"albumId"]) {
        albumInfo.albumId = self.content;
    } else if ([elementName isEqualToString:@"totalNum"]) {
        albumInfo.totalNum = self.content;
    } else if ([elementName isEqualToString:@"playtimes"]) {
        albumInfo.playtimes = self.content;
    } else if ([elementName isEqualToString:@"digTimes"]) {
        albumInfo.digTimes = self.content;
    } else if ([elementName isEqualToString:@"playlistCount"]) {
        albumInfo.playlistCount = self.content;
    } else if ([elementName isEqualToString:@"year"]) {
        albumInfo.year = self.content;
    } else if ([elementName isEqualToString:@"area"]) {
        albumInfo.area = self.content;
    } else if ([elementName isEqualToString:@"shortDesc"]) {
        albumInfo.shortDesc = self.content;
    } else if ([elementName isEqualToString:@"desc"]) {
        albumInfo.desc = self.content;
    } else if ([elementName isEqualToString:@"language"]) {
        albumInfo.language = self.content;
    } else if ([elementName isEqualToString:@"serial"]) {
        albumInfo.serial = self.content;
    } else if ([elementName isEqualToString:@"hdType"]) {
        albumInfo.hdType = self.content;
    } else if ([elementName isEqualToString:@"cartoonType"]) {
        albumInfo.cartoonType = self.content;
    } else if ([elementName isEqualToString:@"outerPlayerUrl"]) {
        albumInfo.outerPlayerUrl = self.content;
    } else if ([elementName isEqualToString:@"channelId"]) {
        albumInfo.channelId = self.content;
    } else if ([elementName isEqualToString:@"superAlbumId"]) {
        albumInfo.superAlbumId = self.content;
    } else if ([elementName isEqualToString:@"commentCount"]) {
        albumInfo.commentCount = self.content;
    } else if ([elementName isEqualToString:@"totalTime"]) {
        albumInfo.totalTime = self.content;
    } else if ([elementName isEqualToString:@"currentUpdate"]) {
        albumInfo.currentUpdate = self.content;
    } else if ([elementName isEqualToString:@"agegroup"]) {
        albumInfo.agegroup = self.content;
    } else if ([elementName isEqualToString:@"actors"]) {
        albumInfo.actors = self.contentArray;
        self.contentArray = nil;
    } else if ([elementName isEqualToString:@"directors"]) {
        albumInfo.directors = self.contentArray;
        self.contentArray = nil;
    } else if ([elementName isEqualToString:@"categories"]) {
        albumInfo.categories = self.contentArray;
        self.contentArray = nil;
    } else if ([elementName isEqualToString:@"itemCodes"]) {
        albumInfo.itemCodes = self.contentArray;
        self.contentArray = nil;
    }
}


/** 结束解析 */
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    
}
@end
